using System.Net;
using LocalOverride.Proxy;
using NUnit.Framework;

namespace LocalOverride.Tests.Proxy
{
    [TestFixture]
    public class ProxyTests
    {
        [Test]
        public void CanInterceptWebRequests()
        {
            var proxy = new HttpProxy(8889);

            string requestedUrl = null;

            string resultString = "test string";

            proxy.Start(
                (requestUrl) =>
                {
                                 requestedUrl = requestUrl;
                                 return new InterceptResult { ResultType = InterceptResultType.String, Response = resultString };
                }

    );

            string result;
            string googleUrl = "http://google.com/blah.html";
            try
            {
                var client = new WebClient
                                 {
                                     Proxy = new WebProxy("http://localhost:8889", false)
                                 };
                
                result = client.DownloadString(googleUrl);
            }
            finally
            {
                proxy.Stop();
            }
            Assert.AreEqual(googleUrl, requestedUrl);
            
            Assert.AreEqual(resultString, result);
        }

        [Test]
        public void CanSendFiles()
        {
            var proxy = new HttpProxy(8889);

            string requestedUrl = null;

            proxy.Start(
                requestUrl =>
                {
                    requestedUrl = requestUrl;
                    return new InterceptResult { ResultType = InterceptResultType.File, FileName = @"c:\\temp\\test.html"};
                }

            );

            string result;
            string googleUrl = "http://google.com/blah.html";
            try
            {
                var client = new WebClient
                {
                    Proxy = new WebProxy("http://localhost:8889", false)
                };

                result = client.DownloadString(googleUrl);
            }
            finally
            {
                proxy.Stop();
            }

            string resultString =
                @"<html>
<head>
<title>Test</title>
</head>
<body>
Hello Fiddler!
</body>
</html>";
            Assert.AreEqual(resultString, result);
        }
    }
}